import router from './router'
import store from './store'
import {
  Message
} from 'element-ui'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式
import {
  getToken
} from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({
  showSpinner: false
}) // NProgress Configuration

const whiteList = ['/login'] // 不重定向白名单
const asyncRoutes = [
  {
    "title": "首页",
    "name": "system",
    "icon": "dashboard",
    "path": "system",
    "activeMenu": "",
    "redirect": "/system/baseData",
    "buttons": [],
    "hidden": false,
    "children": [
      {
        "title": "头部图片",
        "name": "model1",
        "icon": "",
        "path": "model1",
        "activeMenu": "system",
        "redirect": "",
        "hidden": false,
        "children": []
      },
      {
        "title": "首页轮播",
        "name": "model2",
        "icon": "",
        "path": "model2",
        "activeMenu": "system",
        "redirect": "",
        "hidden": false,
        "children": []
      },
      {
        "title": "产品服务",
        "name": "model3",
        "icon": "",
        "path": "model3",
        "activeMenu": "system",
        "redirect": "",
        "hidden": false,
        "children": []
      },
      {
        "title": "智赋百业",
        "name": "model4",
        "icon": "",
        "path": "model4",
        "activeMenu": "system",
        "redirect": "",
        "hidden": false,
        "children": []
      },
      {
        "title": "特色优势",
        "name": "model5",
        "icon": "",
        "path": "model5",
        "activeMenu": "system",
        "redirect": "",
        "hidden": false,
        "children": []
      },
      {
        "title": "合作客户",
        "name": "model6",
        "icon": "",
        "path": "model6",
        "activeMenu": "system",
        "redirect": "",
        "hidden": false,
        "children": []
      },
      {
        "title": "关于我们",
        "name": "model7",
        "icon": "",
        "path": "model7",
        "activeMenu": "system",
        "redirect": "",
        "hidden": false,
        "children": []
      },
      {
        "title": "软件下载",
        "name": "model9",
        "icon": "",
        "path": "model9",
        "activeMenu": "system",
        "redirect": "",
        "hidden": false,
        "children": []
      },
      {
        "title": "使用文档",
        "name": "model8",
        "icon": "",
        "path": "model8",
        "activeMenu": "system",
        "redirect": "",
        "hidden": false,
        "children": []
      },
    ]
  }
 
]
router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start()

  // 设置网页title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  const hasToken = getToken('szerp-loginToken')

  if (hasToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({
        path: '/'
      })
      NProgress.done()
    } else {
      const routes = store.getters.routes && store.getters.routes.length > 0
      if (routes) {
        next()
      } else {
        try {
          // get user info
          // const asyncRoutes = await store.dispatch('user/getInfo')
          // const {
          //   permissionMenuData
          // } = await store.dispatch('user/getInfo')
          // const accessRoutes = await store.dispatch('permission/generateRoutes', permissionMenuData)
          // router.addRoutes(accessRoutes)
          store.dispatch('user/getInfo')
          const accessRoutes = await store.dispatch('permission/generateRoutes', asyncRoutes)
          router.addRoutes(accessRoutes)
          next({
            ...to,
            replace: true
          })
        } catch (error) {
          // 删除token并转到登录页以重新登录
          await store.dispatch('user/resetToken')
          Message.error(error || 'Has Error')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/

    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单中，直接进入
      next()
    } else {
      // 其他没有访问权限的页面将重定向到登录页面。
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
